package com.zcm.modules.zhaocai.utils;

import com.google.gson.Gson;
import com.zcm.common.config.Constant;
import com.zcm.common.utils.DateUtils;
import com.zcm.common.utils.StringUtils;
import com.zcm.modules.zhaocai.entity.*;

import java.util.*;

public class DDDataUtils {
    static public void parseKpData(ZcmKanpanData zcmKanpanData,Dadata dadata){

        Gson gson = new Gson();
        if(zcmKanpanData==null){
            return;
        }
        if("boardStockTask".equals(zcmKanpanData.getType())){
            DDSourceEntity entity = gson.fromJson(zcmKanpanData.getContent(),DDSourceEntity.class);
            DDTableDataEntity data = entity.getData();
            Map<String,Integer> titleIndex = new HashMap();
            int i = 0;
            for (String field : data.getFields()) {
                titleIndex.put(field,i++);
            }
            List<ZTB> sthsztbs = new ArrayList<ZTB>();
            List<ZTB> yzbs = new ArrayList<ZTB>();
            List<ZTB> lbggs = new ArrayList<ZTB>();
            for (List<Object> item : data.getItems()) {
                ZTB ztb = new ZTB();
                String dm = item.get(titleIndex.get("symbol")).toString();
                String mc = item.get(titleIndex.get("title")).toString();
                Integer lbs = ((Double)item.get(titleIndex.get("continue_board_count"))).intValue();
                Double fbsj = (Double)item.get(titleIndex.get("last_raise_time"));
                String yy = item.get(titleIndex.get("reason")).toString();
                dm = dm.replace(".SS","").replace(".SZ","");

                if(StringUtils.isEmpty(yy)) {
                    yy = "请关注微信" + Constant.DUODUO_MPACCOUNT;
                }else{
                    yy = yy.replace("选股宝", "蔡天真").replace("脱水研报", "蔡天真");
                }
                Date date = new Date();
                date.setTime(fbsj.longValue() * 1000);
                ztb.setDm(dm);
                ztb.setLbs(lbs);
                ztb.setMc(mc);
                ztb.setFbsj(DateUtils.formatDate(date,"HH:mm:ss"));
                ztb.setYy(yy);

                if (Integer.parseInt(DateUtils.formatDate(date,"HHmmss")) <= 93003) {
                    yzbs.add(ztb);
                }
                if (lbs > 1) {
                    lbggs.add(ztb);
                }else {
                    sthsztbs.add(ztb);
                }

            }
            dadata.setLbggs(lbggs);
            dadata.setSthsztb(sthsztbs);
            dadata.setYzbs(yzbs);
        }else if("boomStockTask".equals(zcmKanpanData.getType())){
            DDSourceEntity entity = gson.fromJson(zcmKanpanData.getContent(),DDSourceEntity.class);
            DDTableDataEntity data = entity.getData();
            int raiseCount = dadata.getSthsztb().size() + dadata.getLbggs().size();
            int boomCount = data.getItems().size();
            Double zbl = boomCount/((double)(raiseCount + boomCount));
            dadata.setZbl( String.format("%.2f%%",100 * zbl));
            Map<String,Integer> titleIndex = new HashMap();
            int i = 0;
            for (String field : data.getFields()) {
                titleIndex.put(field,i++);
            }
            List<BZKB> bzkbs = new ArrayList<BZKB>();
            for (List<Object> item : data.getItems()) {

                BZKB bzkb = new BZKB();
                String dm = item.get(titleIndex.get("symbol")).toString();
                String mc = item.get(titleIndex.get("title")).toString();
                Integer lbs = ((Double)item.get(titleIndex.get("continue_board_count"))).intValue();
                Double fbsj = (Double)item.get(titleIndex.get("last_boom_time"));
                String zdf = item.get(titleIndex.get("px_change_rate")).toString();
                String yy = item.get(titleIndex.get("reason")).toString();
                dm = dm.replace(".SS","").replace(".SZ","");

                if(StringUtils.isEmpty(yy)) {
                    yy = "请关注微信" + Constant.DUODUO_MPACCOUNT;
                }else{
                    yy = yy.replace("选股宝", Constant.DUODUO_MPACCOUNT).replace("脱水研报", Constant.DUODUO_MPACCOUNT);
                }
                Date date = new Date();
                date.setTime(fbsj.longValue() * 1000);
                bzkb.setDm(dm);
                bzkb.setLbs(lbs);
                bzkb.setMc(mc);
                bzkb.setZbsj(DateUtils.formatDate(date,"HH:mm:ss"));
                bzkb.setYy(yy);
                bzkb.setZdf(zdf);
                bzkbs.add(bzkb);
            }
            dadata.setBzkbs(bzkbs);
        }else if("boardDownTask".equals(zcmKanpanData.getType())){
            DDSourceEntity entity = gson.fromJson(zcmKanpanData.getContent(),DDSourceEntity.class);
            DDTableDataEntity data = entity.getData();

            Map<String,Integer> titleIndex = new HashMap();
            int i = 0;
            for (String field : data.getFields()) {
                titleIndex.put(field,i++);
            }
            List<DTB> dtbs = new ArrayList<DTB>();
            for (List<Object> item : data.getItems()) {
                DTB dtb = new DTB();

                String dm = item.get(titleIndex.get("symbol")).toString();
                dm = dm.replace(".SS","").replace(".SZ","");

                String mc = item.get(titleIndex.get("title")).toString();
                Double hsl = ((Double)item.get(titleIndex.get("turnover_ratio")));
                Double zdf = ((Double)item.get(titleIndex.get("px_change_rate")));

                dtb.setDm(dm);
                dtb.setMc(mc);
                dtb.setZdf(String.format("%.2f%%",zdf));
                dtb.setHsl(String.format("%.2f%%",hsl));
                dtbs.add(dtb);
            }
            dadata.setDtbs(dtbs);
        }else if("boardPredayTask".equals(zcmKanpanData.getType())){
            DDSourceEntity entity = gson.fromJson(zcmKanpanData.getContent(),DDSourceEntity.class);
            DDTableDataEntity data = entity.getData();

            Map<String,Integer> titleIndex = new HashMap();
            int i = 0;
            for (String field : data.getFields()) {
                titleIndex.put(field,i++);
            }

            double sumPX = 0.0f;
            List<ZRZT> zrzts = new ArrayList<ZRZT>();
            for (List<Object> item : data.getItems()) {
                ZRZT zrzt = new ZRZT();

                String dm = item.get(titleIndex.get("symbol")).toString();
                dm = dm.replace(".SS","").replace(".SZ","");

                String mc = item.get(titleIndex.get("title")).toString();
                Double hsl = ((Double)item.get(titleIndex.get("turnover_ratio")));
                Double zdf = ((Double)item.get(titleIndex.get("px_change_rate")));

                Integer lbs = ((Double)item.get(titleIndex.get("continue_board_count"))).intValue();
                String yy = item.get(titleIndex.get("reason")).toString();
                if(StringUtils.isEmpty(yy)) {
                    yy = "请关注微信" + Constant.DUODUO_MPACCOUNT;
                }else{
                    yy = yy.replace("选股宝", Constant.DUODUO_MPACCOUNT).replace("脱水研报", Constant.DUODUO_MPACCOUNT);
                }
                zrzt.setDm(dm);
                zrzt.setMc(mc);
                zrzt.setZdf(String.format("%.2f%%",zdf));
                zrzt.setHsl(String.format("%.2f%%",hsl));
                zrzt.setYy(yy);
                zrzt.setZrlbs(lbs);
                zrzts.add(zrzt);
                sumPX += zdf;
            }
            dadata.setZrzts(zrzts);
            dadata.setZrztgzs(String.format("%.2f%%",zrzts.size() == 0? 0f : sumPX/zrzts.size()));
        }else if("multiStockTask".equals(zcmKanpanData.getType())){
            DDSourceEntity entity = gson.fromJson(zcmKanpanData.getContent(),DDSourceEntity.class);
            DDTableDataEntity data = entity.getData();

            Map<String,Integer> titleIndex = new HashMap();
            int i = 0;
            for (String field : data.getFields()) {
                titleIndex.put(field,i++);
            }
            List<QSG> qsgs = new ArrayList<QSG>();
            for (List<Object> item : data.getItems()) {
                QSG qsg = new QSG();

                String dm = item.get(titleIndex.get("symbol")).toString();
                dm = dm.replace(".SS","").replace(".SZ","");

                String mc = item.get(titleIndex.get("title")).toString();
                Double hsl = ((Double)item.get(titleIndex.get("turnover_ratio")));
                Double zdf = ((Double)item.get(titleIndex.get("px_change_rate")));

                Integer lbs = ((Double)item.get(titleIndex.get("continue_board_count"))).intValue();
                Integer board_days = ((Double)item.get(titleIndex.get("board_days"))).intValue();
                Integer board_count = ((Double)item.get(titleIndex.get("board_count"))).intValue();
                String yy = item.get(titleIndex.get("reason")).toString();
                if(StringUtils.isEmpty(yy)) {
                    yy = "请关注微信" + Constant.DUODUO_MPACCOUNT;
                }else{
                    yy = yy.replace("选股宝", Constant.DUODUO_MPACCOUNT).replace("脱水研报", Constant.DUODUO_MPACCOUNT);
                }
                qsg.setDm(dm);
                qsg.setMc(mc);
                qsg.setZdf(String.format("%.2f%%",zdf));
                qsg.setHsl(String.format("%.2f%%",hsl));
                qsg.setYy(yy);
                qsg.setJtjb(String.format("%d天%d板",board_days,board_count));
                qsgs.add(qsg);
            }
            dadata.setQsgs(qsgs);

        }else if("realTask".equals(zcmKanpanData.getType())){
            DDRealSourceEntity entity = gson.fromJson(zcmKanpanData.getContent(),DDRealSourceEntity.class);
            Map<String,Object> data = (Map)entity.getData();
            Map<String,Map<String,Object> > snapshot = (Map)data.get("snapshot");
            List<Object> shangz =(List)snapshot.get("000001.SS");
            //List<Object> fields =(List)snapshot.get("fields");
            List<Object> shengz =(List)snapshot.get("399001.SZ");
            List<Object> cy =(List)snapshot.get("399006.SZ");
            dadata.setShangz(String.format("%.2f%% %.2f%%",shangz.get(0),shangz.get(1)));
            dadata.setShengz(String.format("%.2f%% %.2f%%",shengz.get(0),shengz.get(1)));
            dadata.setCy(String.format("%.2f%% %.2f%%",cy.get(0),cy.get(1)));
//
//            Map<String,Integer> titleIndex = new HashMap();
//            int i = 0;
//            for (String field : data.getFields()) {
//                titleIndex.put(field,i++);
//            }
//            dadata.setShangz(data.getSnapshot());
//            dadata.setShengz(data.getSnapshot());
//            dadata.setCy(data.getSnapshot());
        }else if("quoteChangeTask".equals(zcmKanpanData.getType())){
            DDQuoteSourceEntity entity = gson.fromJson(zcmKanpanData.getContent(),DDQuoteSourceEntity.class);
            Map<String, Object> data = entity.getData();
            Double declineNum = (Double) data.get("decline_num_count");
            Double stableNum = (Double) data.get("stable_stocks_count");
            Double surgedNum = (Double) data.get("surged_num_count");
            dadata.setZhang(surgedNum.intValue());
            dadata.setDie(declineNum.intValue());
            dadata.setPing(stableNum.intValue());
        }
    }
}
